package com.fxnotes;

import com.fxnotes.command.ShowBooks;
import com.fxnotes.common.ui.NavigationPathBuilder;
import com.fxnotes.database.inmemory.repository.EntityDuplicateException;
import com.fxnotes.domain.model.Book;
import com.fxnotes.domain.model.Chapter;
import com.fxnotes.domain.model.Page;
import com.fxnotes.domain.repository.IBookRepository;
import com.fxnotes.domain.repository.IChapterRepository;
import com.fxnotes.domain.repository.IPageRepository;
import com.google.common.eventbus.EventBus;
import com.google.inject.Guice;
import com.google.inject.Injector;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MainApp extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws EntityDuplicateException {
        Injector injector = Guice.createInjector(new GuiceModule());
        AppController appController = injector.getInstance(AppController.class);
        appController.init();

        primaryStage.setScene(new Scene(appController.getNode(), 700, 500));
        primaryStage.setTitle(Resources.APP_TITLE);
        primaryStage.show();

        // init data
        IBookRepository bookRepository = injector.getInstance(IBookRepository.class);
        IChapterRepository chapterRepository = injector.getInstance(IChapterRepository.class);
        IPageRepository pageRepository = injector.getInstance(IPageRepository.class);
        
        Book book = new Book("Effective Java");
        bookRepository.add(book);
        
        Chapter chapter = new Chapter("Creating and Destroying objects");
        chapterRepository.add(book, chapter);
        
        pageRepository.add(chapter, new Page("Consider using static factory methods instead of constructors"));
        injector.getInstance(EventBus.class).post(new ShowBooks());
        
    }
}
